// AboutDlg.cpp : implementation of the CGroupsDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#ifdef WIN32_PLATFORM_PSPC
#include "resourceppc.h"
#else
#include "resourcesp.h"
#endif
#include "memberinfodlg.h"
#include "utils.h"

static const int MAX_NAME_EDIT_TEXT = 256;
static const int MAX_NUMBER_EDIT_TEXT = 128;

CMemberInfoDlg::CMemberInfoDlg()
    : m_ContactId(0)
    , m_PropertyId(0)
{
}

LRESULT CMemberInfoDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
    AtlCreateMenuBar(m_hWnd, IDM_CONTACTS_CANCEL);

#ifdef WIN32_PLATFORM_WFSP
    if (hMenuBar != NULL)
        AtlActivateBackKey(hMenuBar);
#endif

    m_NameEdit.Attach(GetDlgItem(IDC_EDT_MEMBER_NAME));
    m_NameEdit.LimitText(MAX_NAME_EDIT_TEXT);
    m_NameEdit.SetFocus();

    m_NumberEdit.Attach(GetDlgItem(IDC_EDT_MEMBER_NUMBER));
    m_NumberEdit.LimitText(MAX_NUMBER_EDIT_TEXT);

    if (m_ContactId != 0)
    {
        HRESULT hr = Utils::GetContactDetails(
            m_ContactId,
            m_PropertyId,
            &m_Name,
            &m_Number);
        if (SUCCEEDED(hr))
        {
            m_NameEdit.SetWindowText(m_Name);
            m_NumberEdit.SetWindowText(m_Number);
        }
    }

    UpdateUI();

    return bHandled = FALSE;
}

LRESULT CMemberInfoDlg::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
    return bHandled = FALSE;
}

LRESULT CMemberInfoDlg::OnMenuContacts(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    HRESULT hr = Utils::ChooseContact(
        m_hWnd,
        &m_Name,
        &m_Number,
        &m_ContactId,
        &m_PropertyId);
    if (FAILED(hr))
    {
        ATLTRACE(_T("Utils::ChooseContact failed\n"));
        return ERROR_GEN_FAILURE;
    }

    m_NameEdit.SetWindowText(m_Name);
    m_NumberEdit.SetWindowText(m_Number);

    return 0;
}

LRESULT CMemberInfoDlg::OnMenuCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    StdCloseDialog(ID_MENU_CANCEL);
    return 0;
}

LRESULT CMemberInfoDlg::OnEdtMemberNameChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    UpdateUI();
    return 0;
}

LRESULT CMemberInfoDlg::OnEdtMemberNumberChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    UpdateUI();
    return 0;
}

LRESULT CMemberInfoDlg::OnOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
    if (m_NameEdit.GetWindowTextLength() == 0 ||
        m_NumberEdit.GetWindowTextLength() == 0)
    {
        Utils::MessageBoxRes(
            m_hWnd,
            IDR_MAINFRAME,
            IDS_SELECT_CONTACT,
            MB_ICONEXCLAMATION | MB_OK);
        bHandled = TRUE;
        return TRUE;
    }

    bHandled = FALSE;
    return 0;
}

void CMemberInfoDlg::UpdateUI(void)
{
    bool Enable =
        m_NameEdit.GetWindowTextLength() > 0 &&
        m_NumberEdit.GetWindowTextLength() > 0;

    Utils::EnableMenuItem(this->m_hWnd, ID_MENU_OK, Enable);
}
